package com.cnc.fd.web;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
 

import com.cnc.bas.query.PageInfo;
import com.cnc.bas.util.Constants;
import com.cnc.bas.util.LTUtil;
import com.cnc.bas.web.BasMapForm;
import com.cnc.bas.web.SecureBaseDispatchAction;
import com.cnc.fd.hiber.FdCustomerConfig;
import com.cnc.fd.service.FdDao;
import com.cnc.fd.service.IFdService;
import com.cnc.fd.sqlmapDao.ICustomerConfDao;
import com.cnc.ssm.UserInfo;


public class CustomerConfAction extends SecureBaseDispatchAction {

	private ICustomerConfDao customerConfDao = null;
	private FdDao fdDao = null;
	private IFdService service = null;
	private static final int defaultPageSize = 15;
	private static final int maxPageSize = 2000;

	public void setServlet(ActionServlet actionServlet) {
		super.setServlet(actionServlet);
		customerConfDao = (ICustomerConfDao) this.getBean(ICustomerConfDao.BEAN_NAME);
		fdDao = (FdDao) this.getBean(FdDao.BEAN_ID);
		service = (IFdService) this.getBean(IFdService.BEAN_ID);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String customer=request.getParameter("customer");
		
		//写入操作日志
		HashMap log = new HashMap();
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
				Constants.USER_KEY);
		log.put("OPER_CODE", userInfo.getUserCode());
		log.put("OPER_NAME", userInfo.getUserName());
		log.put("ACTION_LOG", "删除客户 "+customer+" 的配置");
		 
		customerConfDao.deleteCustomerConf(customer);
		return this.list(mapping, form, request, response);
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap hs = ((BasMapForm) form).getHs();
		//是否更改了用户
		String aux=(String)(hs.get("customer"));
		HashMap map=customerConfDao.getCustomerConfByCustomer(aux);
		if(null!=map)
		{
			//写入操作日志
			HashMap actionlog = new HashMap();
			UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
					Constants.USER_KEY);
			actionlog.put("OPER_CODE", userInfo.getUserCode());
			actionlog.put("OPER_NAME", userInfo.getUserName());
			actionlog.put("ACTION_LOG", "修改了客户 "+aux+" 的配置");
			 
	//		customerConfDao.updateCustomerConf(hs);
			String customer=hs.get("customer").toString();
			String priority=hs.get("priority").toString();
			String allowUploadTaskCount=hs.get("allowUploadTaskCount").toString();
			String allowOtherTaskCount=hs.get("allowOtherTaskCount").toString();
			String newTaskFirst=hs.get("newTaskFirst").toString();
			String allowFailureCount=hs.get("allowFailureCount").toString();
			String isPurge=hs.get("isPurge").toString();
			String dayUploadSyncCount=hs.get("dayUploadSyncCount").toString();
			String wsCPOp=hs.get("wsOP").toString();
			String fileUID=hs.get("fileUID").toString();
			String ops="rename,upload,delete,mkdir";
			if(StringUtils.isNotEmpty((String)hs.get("isGrantUnzip"))&&"on".equals((String)hs.get("isGrantUnzip")))
			{
				ops+=",unzip";
			}
			if(StringUtils.isNotEmpty((String)hs.get("isGrantCopy"))&&"on".equals((String)hs.get("isGrantCopy")))
			{
				ops+=",copy";
			}
			FdCustomerConfig customerConf= new FdCustomerConfig();
			if(StringUtils.isNotEmpty(customer)){
				customerConf.setCustomer(customer);
			}else{
				return mapping.findForward("main");
			}
			try{
			if(StringUtils.isNotEmpty(priority)){
				customerConf.setPriority(new Short(priority));
			}
			if(StringUtils.isNotEmpty(allowUploadTaskCount)){
				customerConf.setAllowUploadTaskCount(new Short(allowUploadTaskCount));
			}
			if(StringUtils.isNotEmpty(allowOtherTaskCount)){
				customerConf.setAllowOtherTaskCount(new Short(allowOtherTaskCount));
			}
			if(StringUtils.isNotEmpty(newTaskFirst)){
				customerConf.setNewTaskFirst(newTaskFirst);
			}
			if(StringUtils.isNotEmpty(allowFailureCount)){
				customerConf.setAllowFailureCount(new Integer(allowFailureCount));
			}
			if(StringUtils.isNotEmpty(isPurge)){
				customerConf.setIsPurge(isPurge);
			}
			if(StringUtils.isNotEmpty(dayUploadSyncCount)){
				customerConf.setDayUploadSyncCount(new Integer(dayUploadSyncCount));
			}
			if(StringUtils.isNotEmpty(wsCPOp)){
				customerConf.setWsCP_Oper(wsCPOp);
			}
			if(StringUtils.isNotEmpty(fileUID)){
				customerConf.setFileAttribute(fileUID);
			}
			if(StringUtils.isNotEmpty(ops)){
				customerConf.setOperRight(ops);
			}
			}catch(Exception e){
				log.error("NumberFormatException: "+e.toString());
				throw e;
			}
		 
			
		}
		return mapping.findForward("main");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap hs = ((BasMapForm) form).getHs();

		HashMap map = customerConfDao.getCustomerConfByCustomer((String) (hs
				.get("customer")));
		if (null == map) {
			// 写入操作日志
			// HashMap actionlog = new HashMap();
			// UserInfo userInfo = (UserInfo) request.getSession()
			// .getAttribute(Constants.USER_KEY);
			// actionlog.put("OPER_CODE", userInfo.getUserCode());
			// actionlog.put("OPER_NAME", userInfo.getUserName());
			// actionlog.put("ACTION_LOG", "修改了客户 " + customer + " 的配置");
			// service.addActionLog(actionlog);

			String customer = hs.get("customer").toString();
			String priority = hs.get("priority").toString();
			String allowUploadTaskCount = hs.get("allowUploadTaskCount")
					.toString();
			String allowOtherTaskCount = hs.get("allowOtherTaskCount")
					.toString();
			String newTaskFirst = hs.get("newTaskFirst").toString();
			String allowFailureCount = hs.get("allowFailureCount").toString();
			 String isPurge=hs.get("isPurge").toString();
			String dayUploadSyncCount = hs.get("dayUploadSyncCount").toString();
			String wsCPOp = hs.get("wsOP").toString();
			String rootDir = hs.get("rootDir").toString();
			String fileUID = hs.get("fileUID").toString();
			String ops = "rename,upload,delete,mkdir";
			if (StringUtils.isNotEmpty((String) hs.get("isGrantUnzip"))
					&& "on".equals((String) hs.get("isGrantUnzip"))) {
				ops += ",unzip";
			}
			if (StringUtils.isNotEmpty((String) hs.get("isGrantCopy"))
					&& "on".equals((String) hs.get("isGrantCopy"))) {
				ops += ",copy";
			}
			FdCustomerConfig customerConf = new FdCustomerConfig();
			if (StringUtils.isNotEmpty(customer)) {
				customerConf.setCustomer(customer);
			} else {
				return mapping.findForward("main");
			}
			try {
				if (StringUtils.isNotEmpty(priority)) {
					customerConf.setPriority(new Short(priority));
				}
				if (StringUtils.isNotEmpty(allowUploadTaskCount)) {
					customerConf.setAllowUploadTaskCount(new Short(
							allowUploadTaskCount));
				}
				if (StringUtils.isNotEmpty(allowOtherTaskCount)) {
					customerConf.setAllowOtherTaskCount(new Short(
							allowOtherTaskCount));
				}
				if (StringUtils.isNotEmpty(newTaskFirst)) {
					customerConf.setNewTaskFirst(newTaskFirst);
				}
				if (StringUtils.isNotEmpty(allowFailureCount)) {
					customerConf.setAllowFailureCount(new Integer(
							allowFailureCount));
				}
				 if(StringUtils.isNotEmpty(isPurge)){
				 customerConf.setIsPurge(isPurge);
				 }
				if (StringUtils.isNotEmpty(dayUploadSyncCount)) {
					customerConf.setDayUploadSyncCount(new Integer(
							dayUploadSyncCount));
				}
				if (StringUtils.isNotEmpty(wsCPOp)) {
					customerConf.setWsCP_Oper(wsCPOp);
				}
				if (StringUtils.isNotEmpty(fileUID)) {
					customerConf.setFileAttribute(fileUID);
				}
				if (StringUtils.isNotEmpty(ops)) {
					customerConf.setOperRight(ops);
				}
			} catch (Exception e) {
				log.error("NumberFormatException: " + e.toString());
				throw e;
			}
		 
			return mapping.findForward("main");
		} else {
			return this.update(mapping, form, request, response);
		}
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List list=customerConfDao.getCustomerConfByNameFuzzy(null);
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String customer=request.getParameter("customer");
		//
		HashMap hs=customerConfDao.getCustomerConfByCustomer(customer);
		
		if(null==hs){
			return this.list(mapping,form,request,response);
		}

		String ops=(String)hs.get("operRight");
		if(ops.contains("unzip"))
		{
			hs.put("isGrantUnzip","on");
		}else{
			hs.put("isGrantUnzip","off");
		}
		if(ops.contains("copy"))
		{
			hs.put("isGrantCopy","on");
		}else{
			hs.put("isGrantCopy","off");
		}
		hs.put("wsOP",hs.get("wsCP_Oper"));
		hs.put("fileUID",hs.get("fileAttribute"));
		hs.put("isPurge", hs.get("isPurge"));
		((BasMapForm) form).setHs(hs);
		List list=fdDao.getWsfdAccount();
		request.setAttribute("customers",list);
		request.setAttribute("operType","edit");
		return mapping.findForward("edit");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List list=fdDao.getWsfdAccount();
		request.setAttribute("customers",list);
		request.setAttribute("operType","create");
		return mapping.findForward("edit");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap hs = ((BasMapForm) form).getHs();
		String customer=(String)hs.get("customer");
		int intCurrentPage = 1;
		String currPage = request.getParameter("intCurrentPage");
		try{
			intCurrentPage = Integer.parseInt(currPage);
		}
		catch(Exception e){
		}
		
		int intTotalPages = 1;
		String totalPage = request.getParameter("intTotalPages");
		try{
			intTotalPages = Integer.parseInt(totalPage);
		}
		catch(Exception e){
			
		}
	 
		int pageSize = (intTotalPages == -1) ? maxPageSize : defaultPageSize;
		PageInfo page = new PageInfo(intCurrentPage, pageSize);
		List list=customerConfDao.getCustomerConfByNameFuzzy(customer);
		//request.setAttribute("list",list);
		page.setPageData(list);
		page.setTotalCount(customerConfDao.getTotalCount());
		LTUtil.setPageData(request, page);
		return mapping.findForward("list");
	}
}
